/*
 * Staff.java
 *
 * Created on 11. Juni 2007, 03:32
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.fact;

import corso.lang.*;

import sbc.corso.CorsoConnectionHolder;
import sbc.util.*;
import java.util.*;
/**
 *
 * @author mustermark
 */
public class Staff extends CorsoConnectionHolder implements CorsoShareable, Identifiable {
    
    private List<CorsoVarOid> workerOids;
    private CorsoVarOid staffOid;
    private Factory fact;
    private String uid;
    private static int cnt;
    
    /**
     * Creates a new instance of Staff
     */
    protected Staff(Factory f) {
        workerOids = new ArrayList<CorsoVarOid>();
        fact = f;
        uid = fact.getUID() + ":Staff_" + cnt++;
        connect();
        createOrGetOids();
    }
    
    /**
     * reads an instance of Staff
     */
    protected Staff(CorsoVarOid oid) {
        System.out.println ("Read Staff = " + oid);
        try {
            oid.readShareable(this, null, CorsoConnection.INFINITE_TIMEOUT);
        } catch (CorsoException e) {
            e.printStackTrace();
        }
    }
    
    public void write (CorsoData data) throws CorsoDataException
    {
        data.putStructTag ("Staff", workerOids.size ());
        for (int i = 0; i < workerOids.size(); i++) data.putShareable ((CorsoVarOid)workerOids.get(i));
    }

    public String getUID() { return uid; }
    //------------------------------------------------------------------------
    // read
    //------------------------------------------------------------------------
    public void read (CorsoData data) throws CorsoDataException
    {
        StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);

        workerOids = new ArrayList<CorsoVarOid>();
        for (int i = 0; i < arity; i++)
        {
            CorsoVarOid oid = new CorsoVarOid ();
            data.getShareable (oid);
            System.out.println ("Next Worker = " + oid);
            workerOids.add(oid);
        }
    }
    
    public void engage(Task w) {
        //landebahnenOids = new Vector (nLandebahnen);
        CorsoVarOid oid;
        try {

            oid = con.createVarOid (con.getCurrentStrategy ());
            oid.writeShareable (w, CorsoConnection.INFINITE_TIMEOUT);
            workerOids.add (oid);
        
        } catch (CorsoException eSys1) {   
            System.out.println (eSys1);
            System.exit (-1);
        }
    }
    
    public int getMemberCount() { return workerOids.size(); }
    
    public List<CorsoVarOid> getWorkerOids() { return workerOids; }
 
     public void createOrGetOids() {
        //CorsoVarOid namedOid = null;
       
        
        try {
            staffOid = super.getDefaultNamedVarOid (getUID());
            System.out.println ("Staff found:" + staffOid);
            
        }
        catch (CorsoException e1)
        {
            System.out.println ("Staff not found");
            try{
                staffOid = super.createDefaultNamedVarOid(getUID());
                staffOid.writeShareable (this, CorsoConnection.INFINITE_TIMEOUT);
                System.out.println ("New Staff created: " + staffOid);
            } catch (CorsoException e2) {
                System.out.println ("Cannot create named oid " + e2);
                e2.printStackTrace();
                System.exit (-4);
                
            }
        }
        
        
       
    }
    public CorsoVarOid getOid() { return staffOid; }
   
}
